﻿using Common;
using Common.DataModels;
using PLC.Domain.Dto;
using PLC.Domain.Enum;

namespace PLC.Domain.Entity;
/// <summary>
/// 设备配置上绑定的数据
/// </summary>
public class DataConfig : BaseEntity<DataConfig>
{
    /// <summary>
    /// 设备配置id
    /// </summary>
    public long DeviceConfigId { get;  set; }

    /// <summary>
    /// 寄存器位
    /// </summary>
    public string Register { get;  set; }

    /// <summary>
    /// 数据类型
    /// </summary>
    public int DataType { get;  set; }

    /// <summary>
    /// 数据长度
    /// </summary>
    public int DataLength { get;  set; }

    /// <summary>
    /// 数据编码类型
    /// </summary>
    public int? Encoding { get;  set; }
    /// <summary>
    /// 二维码的种类
    /// </summary>
    public int? CodeType { get;  set; }
    /// <summary>
    /// 状态
    /// </summary>
    public int Status { get;  set; }

    /// <summary>
    /// 数据值的描述
    /// </summary>
    public string DataDescription { get;  set; }
    /// <summary>
    /// 配置类型
    /// </summary>
    public int ConfigType { get;  set; }

    /// <summary>
    /// 判断类型(> < = 这些操作)
    /// </summary>
    public int JudgeType { get;  set; }
    /// <summary>
    /// 判断值
    /// </summary>
    public string JudgeValue { get;  set; }
    /// <summary>
    /// 需要触发的操作
    /// </summary>
    public int? Trigger { get;  set; }
    /// <summary>
    /// 二维码校验的长度
    /// </summary>
    public int? CodeLength { get;  set; }
    /// <summary>
    /// 排序值
    /// </summary>
    public int OrderNum { get;  set; }
    /// <summary>
    /// 读数据组
    /// </summary>
    public int? ReadGroup { get;  set; }
    /// <summary>
    /// 写数据组
    /// </summary>
    public int? WriteGroup { get;  set; }
    public DataConfig()
    {
    }
    /// <summary>
    /// 不是字符串的默认构造函数
    /// </summary>
    /// <param name="deviceConfigId">设备配置的id</param>
    /// <param name="register">寄存器位</param>
    /// <param name="status">状态</param>
    /// <param name="dataDescription">值的描述</param>
    /// <param name="configType">读写配置（读取  或者写入,或者触发）</param>
    /// <param name="dataType">数据类型</param>
    /// <param name="creatorId">创建人</param>
    /// <param name="orderNum">排序值</param>
    /// <param name="dataLength">数据的长度(string是使用)</param>
    /// <param name="dataEncoding">数据编码(string时使用)</param>
    /// <param name="codeType">二维码的类型(只有是读取和数据分组是二维码的时候要设置)</param>
    /// <param name="codeLnegth">二维码的长度（只有选择了校验二维码的时候 才有意义）</param>
    public DataConfig(long deviceConfigId, string register, int status, string dataDescription,
        int configType,int dataType,int orderNum ,int codeLnegth ,int codeType =0,int dataLength = 4, long creatorId=0,
        int dataEncoding = (int)EnumDataEncoding.UTF8)
    {
        this.SetDeviceConfigId(deviceConfigId).SetRegister(register).SetStatus(status).SetCodeLength(codeLnegth)
            .SetDataDescription(dataDescription).SetConfigType(configType).SetDataLength(dataLength)
            .SetDataEncoding(dataEncoding).SetDataType(dataType).SetOrderNum(orderNum).SetCodeType(codeType).Build();
    }
    /// <summary>
    /// 默认触发的构造
    /// </summary>
    /// <param name="deviceConfigId">设备配置的id</param>
    /// <param name="register">寄存器位</param>
    /// <param name="status">状态</param>
    /// <param name="dataType">数据类型</param>
    /// <param name="creatorId">创建人</param>
    /// <param name="judgeType">操作的类型</param>
    /// <param name="judgeValue">判断的值的大小</param>
    /// <param name="trigger">触发的操作</param>
    /// <param name="orderNum">排序值</param>
    /// <param name="configType">数据类型</param>
    /// <param name="dataLength">数据长度</param>
    /// <param name="dataEncoding">编码方式</param>
    ///  /// <param name="codeType">二维码的类型(只有是读取和数据分组是二维码的时候要设置)</param>
    ///  /// <param name="codeLnegth">二维码的长度（只有选择了校验二维码的时候 才有意义）</param>
    public DataConfig(long deviceConfigId, string register, int status, int dataType, long creatorId,
        int judgeType, string judgeValue, int trigger,int orderNum, int codeLnegth=0, int codeType = 0, int configType = (int)EnumConfigType.触发,
        int dataLength = 4,
        int dataEncoding = (int)EnumDataEncoding.UTF8)
    {
        this.SetDeviceConfigId(deviceConfigId).SetRegister(register).SetStatus(status).SetConfigType(configType).SetDataLength(dataLength).SetDataEncoding(dataEncoding).SetDataType(dataType).SetJudgeValue(judgeValue).SetCodeLength(codeLnegth)
            .SetTrigger(trigger).SetJudgeType(judgeType).SetOrderNum(orderNum).SetCodeType(codeType).Init(creatorId);
    }

    public override DataConfig Build(long creatorId=0)
    {
        base.Init(creatorId);
        return this;
    }

    public DataConfig SetDeviceConfigId(long deivceConfigId)
    {
        this.DeviceConfigId = deivceConfigId;
        return this;
    }

    public DataConfig SetCodeLength(int codeLength)
    {
        this.CodeLength = codeLength;
        return this;
    }
    public DataConfig SetCodeType(int codeType)
    {
        this.CodeType = codeType;
        return this;
    }
    public DataConfig SetReadGroup(int? readGroup)
    {
        this.ReadGroup = readGroup;
        return this;
    }

    public DataConfig SetWriteGroup(int? writeGroup)
    {
        this.WriteGroup = writeGroup;
        return this;
    }
    public DataConfig SetRegister(string register)
    {
        this.Register = register;
        return this;
    }

    public DataConfig SetStatus(int status)
    {
        this.Status = status;
        return this;
    }

    public DataConfig SetDataDescription(string dataDescription)
    {
        this.DataDescription = dataDescription;
        return this;
    }
    public DataConfig SetDataDescription(List<DescriptionItem> descriptionItems)
    {
        if ( descriptionItems.Count==0)
        {
            this.DataDescription = string.Empty;
        }
        this.DataDescription = JsonHelper.SerializeObject(descriptionItems);
        return this;
    }
    public DataConfig SetConfigType(int configType)
    {
        this.ConfigType = configType;
        return this;
    }

    public DataConfig SetDataType(int dataType)
    {   
        this.DataType = dataType;
        return this;
    }

    public DataConfig SetDataLength(int dataLength)
    {
        this.DataLength = dataLength;
        return this;
    }

    public DataConfig SetDataEncoding(int dataEncoding)
    {
        this.Encoding = dataEncoding;
        return this;
    }

    public DataConfig SetJudgeType(int judgeType)
    {
        this.JudgeType = judgeType;
        return this;
    }

    public DataConfig SetJudgeValue(string judgeValue)
    {
        this.JudgeValue = judgeValue;
        return this;
    }

    public DataConfig SetTrigger(int trigger)
    {
        this.Trigger = trigger;
        return this;
    }
   
    public DataConfig SetIsDelete(bool isDelete)
    {
        base.IsDelete = isDelete;
        return this;
    }

    public DataConfig SetOrderNum(int orderNum)
    {
        this.OrderNum = orderNum;
        return this;
    }
}
   